package com.conan.dailyflickr.utils;

import com.conan.dailyflickr.app.DailyFlickrApplication;

import android.content.Context;

public class LayoutUtil {

	// http://developer.android.com/intl/de/guide/practices/screens_support.html
	public static final int RESOLUTION_HVGA 	= 0;	// 320x480
	public static final int RESOLUTION_WVGA800 	= 1;	// 480x800
	public static final int RESOLUTION_WVGA854 	= 2;	// 480x854
	// Currently we don't support these type of screen at shown below:
	public static final int RESOLUTION_QVGA 	= 3;	// 240x320
	public static final int RESOLUTION_WQVGA400	= 4;	// 240x400
	public static final int RESOLUTION_WQVGA432	= 5;	// 240x432
	
	public static final int DENSITY_DPI_LOW		= 120;
	public static final int DENSITY_DPI_MEDIUM	= 160;
	public static final int DENSITY_DPI_HEIGH	= 240;
	public static final int DENSITY_DPI_LARGE	= 320;	// not support this type of density
	
	public static int DISPLAY_WIDTH_PIXELS 		= 480;
	public static int DISPLAY_HEIGH_PIXELS 		= 800;
	
//	"getApplicationC...trics().density"= 1.5	
//	"getApplicationC...cs().densityDpi"= 240	
//	"getApplicationC...).scaledDensity"= 1.5	
//	"getApplicationC...().heightPixels"= 800	
//	"getApplicationC...s().widthPixels"= 480	
//	"getApplicationC...yMetrics().xdpi"= 254.0	
//	"getApplicationC...yMetrics().ydpi"= 254.0	
//	"LayoutUtil.conv...Context(), 100)"= 150	

	private static int RESOLUTION = RESOLUTION_HVGA;	// default is 320x480
	
	/**
	 * Get resolution of device. 
	 * @return the resolution of our device.
	 */
	public static int getResolution() {
	    return RESOLUTION;
	}
	
	/**
	 * Converts the density independent pixel size to a density dependant pixel size.
	 *
	 * @param dip The intended pixel size.
	 * @return int The converted density dependant pixel size.
	 */
	public static int convertDensity2Pixel(Context context, int dip) {
	  return (int) (dip * context.getResources().getDisplayMetrics().density);
	}
	
	static {
		Context mContext = DailyFlickrApplication.getCustomAppInstance();
		RESOLUTION = RESOLUTION_HVGA;
		if (mContext != null) {
			switch (mContext.getResources().getDisplayMetrics().widthPixels) {
			case 320:
				RESOLUTION = RESOLUTION_HVGA;
				break;
			case 480:
				switch (mContext.getResources().getDisplayMetrics().heightPixels) {
				case 800:
					RESOLUTION = RESOLUTION_WVGA800;
					break;
				case 854:
					RESOLUTION = RESOLUTION_WVGA854;
					break;
				}
				break;
			case 240:
				switch (mContext.getResources().getDisplayMetrics().heightPixels) {
				case 320:
					RESOLUTION = RESOLUTION_QVGA;
					break;
				case 400:
					RESOLUTION = RESOLUTION_WQVGA400;
					break;
				case 432:
					RESOLUTION = RESOLUTION_WQVGA432;
					break;
				}
				break;
			default:
				RESOLUTION = RESOLUTION_HVGA;
				break;
			}
		}
	}
	
	static {
		switch (RESOLUTION) {
		case RESOLUTION_HVGA:
			DISPLAY_WIDTH_PIXELS = 320;
			DISPLAY_HEIGH_PIXELS = 480;
			break;
		case RESOLUTION_QVGA:
			DISPLAY_WIDTH_PIXELS = 240;
			DISPLAY_HEIGH_PIXELS = 320;
			break;
		case RESOLUTION_WQVGA400:
			DISPLAY_WIDTH_PIXELS = 240;
			DISPLAY_HEIGH_PIXELS = 400;
			break;
		case RESOLUTION_WQVGA432:
			DISPLAY_WIDTH_PIXELS = 240;
			DISPLAY_HEIGH_PIXELS = 432;
			break;
		case RESOLUTION_WVGA800:
			DISPLAY_WIDTH_PIXELS = 480;
			DISPLAY_HEIGH_PIXELS = 800;
			break;
		case RESOLUTION_WVGA854:
			DISPLAY_WIDTH_PIXELS = 480;
			DISPLAY_HEIGH_PIXELS = 854;
			break;
		default:
			DISPLAY_WIDTH_PIXELS = 480;
			DISPLAY_HEIGH_PIXELS = 800;
			break;
		}
	}
	
	// TODO	Exam for supporting multi screen resolution:	HVGA|	WVGA800|	WVGA854|	WQVGA|	WQVGA400|	WQVGA432
	private static final int[] VIEW_EFFECT_WIDTH_ARR =	{	320, 	480,		480, 		240,	240,		240 };// (h*2)/3
	private static final int[] VIEW_EFFECT_HEIGH_ARR =	{	280, 	494,		530, 		280,	280,		280 };// (w*2)/3

	public static final int VIEW_EFFECT_MAX_WIDTH	= VIEW_EFFECT_WIDTH_ARR[RESOLUTION];
	public static final int VIEW_EFFECT_MAX_HEIGH	= VIEW_EFFECT_HEIGH_ARR[RESOLUTION];

	public static final int VIEW_EFFECT_MIN_WIDTH	= VIEW_EFFECT_MAX_WIDTH - 100;
	public static final int VIEW_EFFECT_MIN_HEIGH	= VIEW_EFFECT_MAX_HEIGH - 100;

}
